package com.ondrejhrdy.audiorecording;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;

public class MediaRecorderTest implements IRecordingTests {
	private static final String AUDIO_RECORDER_FOLDER = "DiplomA/Comparison/";

	private static MediaRecorder recorder = null;
	private static int currentFormat = 0;
	private static int output_formats[] = { MediaRecorder.OutputFormat.AMR_WB,
			MediaRecorder.OutputFormat.AMR_WB };

	private static boolean isRecording = false;

	private static String getFilename() {
		String filepath = Environment.getExternalStorageDirectory().getPath();
		File file = new File(filepath, AUDIO_RECORDER_FOLDER);

		if (!file.exists()) {
			file.mkdirs();
		}

		return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".awb");
	}

	public void startRecording() {

		MediaRecorderTest.isRecording = true;

		recorder = new MediaRecorder();

		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(output_formats[currentFormat]);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setOutputFile(getFilename());

		try {
			recorder.prepare();
			recorder.start();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void stopRecording() {
		MediaRecorderTest.isRecording = false;
		if (null != recorder) {
			recorder.stop();
			recorder.reset();
			recorder.release();

			recorder = null;
		}
	}

	public boolean isRecording() {
		return MediaRecorderTest.isRecording;
	}

}
